﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using GUIEXAM13.Properties;
using Newtonsoft.Json;

namespace GUIEXAM13
{
    public struct SerializableEvent
    {
        public string Id;
        public string Title;
        public string Comments;
        public List<SerializableClothing> Clothing;
    }

    public class EventDataAccess
    {
        private readonly string _filePath = Environment.CurrentDirectory + @"\events.json";

        public EventDataAccess()
        {
            if (!File.Exists(_filePath))
                File.Create(_filePath).Close();
        }

        public void SaveAllEvents(ObservableCollection<EventDomain> events)
        {
            
            var serializedEvents = new List<SerializableEvent>();

            foreach (var event_ in events)
            {
                //Add all clothing from event_ and add to string list
                var serializedClothing = new List<SerializableClothing>();

                foreach (var clothing_ in event_.Clothing)
                {
                    serializedClothing.Add(new SerializableClothing
                                               {
                                                   Id = clothing_.Id,
                                                   Type = clothing_.Type,
                                                   Color = clothing_.Color,
                                                   Picture = clothing_.Picture
                                               });
                }

                serializedEvents.Add(new SerializableEvent
                                           {
                                               Id = event_.Id,
                                               Title = event_.Title,
                                               Comments = event_.Comments,
                                               Clothing = serializedClothing
                                           });
            }
            
            var _streamWriter = new StreamWriter(_filePath, false);
            _streamWriter.Write(JsonConvert.SerializeObject(serializedEvents));
            _streamWriter.Close();
        }

        public ObservableCollection<EventDomain> GetAllEvents()
        {
            var streamReader = new StreamReader(_filePath);
            var deserializedEvents = new List<SerializableEvent>();
            var returnEventDomainCollection = new ObservableCollection<EventDomain>();

            deserializedEvents = JsonConvert.DeserializeObject<List<SerializableEvent>>(streamReader.ReadToEnd());
            streamReader.Close();

            if (deserializedEvents == null) return returnEventDomainCollection;

            foreach (var deserializedEvent in deserializedEvents)
            {
                var tmpClothingCollection = new ObservableCollection<ClothingDomain>();
                foreach (var clothing in deserializedEvent.Clothing)
                {
                    tmpClothingCollection.Add(new ClothingDomain
                                                  {
                                                      Id = clothing.Id,
                                                      Type = clothing.Type,
                                                      Color = clothing.Color,
                                                      Picture = clothing.Picture
                                                  });
                }

                returnEventDomainCollection.Add(new EventDomain
                                                    {
                                                        Id = deserializedEvent.Id,
                                                        Title = deserializedEvent.Title,
                                                        Comments = deserializedEvent.Comments,
                                                        Clothing = tmpClothingCollection
                                                    });
            }

            return returnEventDomainCollection;
        }
    }
}
